package ch.rheintalconnection.sysventory.presentation.panels;

import ch.rheintalconnection.sysventory.application.Statistic;
import ch.rheintalconnection.sysventory.businessServices.DataService;
import ch.rheintalconnection.sysventory.domain.User;
import ch.rheintalconnection.sysventory.technicalServices.IDataReader;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.VerticalLayout;

public class StatisticPanel extends VerticalLayout{

	private static final long serialVersionUID = 5521379945477399225L;
	@SuppressWarnings("unused")
	private User user;
	private ListSelect listSelect;
	private Button btnShowStatistic;
	private StatisticScansInTimePanel scansInTimePanel;
	private StatisticSoftwarePanel softwarePanel;
	private StatisticComputerPanel computerPanel;
	private StatisticModifyedComputersPanel modifyedComputersPanel;
	
	public StatisticPanel(IDataReader dataReader, DataService dataService){
		
		listSelect = new ListSelect("Statistikauswahl");
		this.addComponent(listSelect);
		
		listSelect.setNullSelectionAllowed(false);
		
		listSelect.addItem(Statistic.ScansInTime);
		listSelect.addItem(Statistic.Software);
		listSelect.addItem(Statistic.Computer);
		listSelect.addItem(Statistic.ModifyedComputers);
	
		btnShowStatistic = new Button("Statistik anzeigen");
		btnShowStatistic.addListener(new ButtonListener());
		btnShowStatistic.setData(this);
		this.addComponent(btnShowStatistic);
		
		scansInTimePanel = new StatisticScansInTimePanel(dataService);
		softwarePanel = new StatisticSoftwarePanel(dataReader);
		computerPanel = new StatisticComputerPanel(dataReader);
		modifyedComputersPanel = new StatisticModifyedComputersPanel(dataService, dataReader);
		
		
		
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void removeStatisticPanels(){
		this.removeComponent(scansInTimePanel);
		this.removeComponent(softwarePanel);
		this.removeComponent(computerPanel);
		this.removeComponent(modifyedComputersPanel);
	}
	
	
	private class ButtonListener implements Button.ClickListener{

		private static final long serialVersionUID = 4352957024410166576L;

		@Override
		public void buttonClick(ClickEvent event) {
			
			StatisticPanel statisticPanel = (StatisticPanel) event.getButton().getData();
			statisticPanel.removeStatisticPanels();
			Statistic statistic = (Statistic) listSelect.getValue();
			
			if(statistic == Statistic.ScansInTime){
				statisticPanel.addComponent(scansInTimePanel);
			}else if (statistic == Statistic.Software) {
				statisticPanel.addComponent(softwarePanel);
			}else if (statistic == Statistic.Computer) {
				statisticPanel.addComponent(computerPanel);
			}else if (statistic == Statistic.ModifyedComputers) {
				statisticPanel.addComponent(modifyedComputersPanel);
			}
			
		}
		
	}

	

}
